<template>
  <view class="shop-info-container">
    <!-- 店铺基础信息 -->
    <view class="shop-header">
      <image :src="shopInfo.logo" mode="aspectFill" class="shop-logo"></image>
      <view class="shop-details">
        <text class="shop-name">{{ shopInfo.name }}</text>
        <view class="shop-rating">
          <u-rate
            :value="shopInfo.rating"
            readonly
            activeColor="#ff5500"
            inactiveColor="#ddd"
            size="28"
          ></u-rate>
          <text class="rating-text">{{ shopInfo.rating }}分</text>
        </view>
        <text class="shop-fans">粉丝数: {{ shopInfo.fansCount }}</text>
      </view>
      <button class="enter-shop-btn" @click="navigateToShop">进店逛逛</button>
    </view>

    <!-- 店铺评分 -->
    <view class="shop-scores">
      <view class="score-item">
        <text class="score-label">描述相符</text>
        <text class="score-value" :class="getScoreClass(shopInfo.scores.description)">{{ shopInfo.scores.description }}</text>
      </view>
      <view class="score-item">
        <text class="score-label">服务态度</text>
        <text class="score-value" :class="getScoreClass(shopInfo.scores.service)">{{ shopInfo.scores.service }}</text>
      </view>
      <view class="score-item">
        <text class="score-label">物流服务</text>
        <text class="score-value" :class="getScoreClass(shopInfo.scores.logistics)">{{ shopInfo.scores.logistics }}</text>
      </view>
    </view>

    <!-- 店铺推荐商品 (可选) -->
    <view class="shop-recommendations" v-if="shopInfo.recommendedProducts && shopInfo.recommendedProducts.length > 0">
      <text class="section-title">店内推荐</text>
      <scroll-view scroll-x class="recommend-scroll">
        <view 
          class="recommend-item" 
          v-for="(product, index) in shopInfo.recommendedProducts"
          :key="index"
          @click="navigateToProduct(product.id)"
        >
          <image :src="product.image" mode="aspectFill" class="recommend-image"></image>
          <text class="recommend-price">¥{{ product.price }}</text>
        </view>
      </scroll-view>
    </view>

    <!-- 联系卖家 -->
    <view class="contact-seller">
      <u-button type="default" icon="chat" @click="contactSeller">联系卖家</u-button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      shopId: '',
      shopInfo: {
        id: '',
        logo: '',
        name: '',
        rating: 0,
        fansCount: 0,
        scores: {
          description: 0,
          service: 0,
          logistics: 0
        },
        recommendedProducts: []
      }
    }
  },
  onLoad(options) {
    // Assuming shopId is passed from the product detail page
    if (options.shopId) {
      this.shopId = options.shopId
      this.loadShopInfo()
    } else {
      console.error('Missing shopId in options')
      uni.showToast({
        title: '缺少店铺信息',
        icon: 'error'
      })
      // Optionally navigate back or handle error
    }
  },
  methods: {
    async loadShopInfo() {
      try {
        // Assuming an API endpoint /shops/{id} exists
        const res = await this.$api.get(`/shops/${this.shopId}`)
        this.shopInfo = res.data
      } catch (error) {
        console.error('加载店铺信息失败:', error)
        uni.showToast({
          title: '加载店铺信息失败',
          icon: 'error'
        })
      }
    },
    navigateToShop() {
      uni.navigateTo({
        url: `/pages/shop/detail?id=${this.shopId}`
      })
    },
    navigateToProduct(productId) {
       uni.navigateTo({
        url: `/pages/product/detail/index?id=${productId}`
      })
    },
    contactSeller() {
      // Implement seller contact logic (e.g., open chat interface)
      uni.showToast({
        title: '功能待开发',
        icon: 'none'
      })
      console.log('Contacting seller...', this.shopId)
    },
    getScoreClass(score) {
      if (score >= 4.7) return 'high'
      if (score < 4.5) return 'low'
      return 'medium'
    }
  }
}
</script>

<style lang="scss" scoped>
.shop-info-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding: 30rpx;
}

.shop-header {
  display: flex;
  align-items: center;
  gap: 20rpx;
  background-color: #ffffff;
  padding: 30rpx;
  border-radius: 12rpx;
  margin-bottom: 20rpx;

  .shop-logo {
    width: 120rpx;
    height: 120rpx;
    border-radius: 50%;
    flex-shrink: 0;
  }

  .shop-details {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 10rpx;

    .shop-name {
      font-size: 32rpx;
      color: #333;
      font-weight: bold;
    }

    .shop-rating {
      display: flex;
      align-items: center;
      gap: 10rpx;

      .rating-text {
        font-size: 24rpx;
        color: #ff5500;
      }
    }

    .shop-fans {
      font-size: 24rpx;
      color: #999;
    }
  }

  .enter-shop-btn {
    height: 60rpx;
    line-height: 60rpx;
    font-size: 24rpx;
    padding: 0 20rpx;
    border: 1rpx solid #ff5500;
    color: #ff5500;
    background-color: #fff1f0;
    border-radius: 30rpx;
  }
}

.shop-scores {
  display: flex;
  justify-content: space-around;
  background-color: #ffffff;
  padding: 30rpx;
  border-radius: 12rpx;
  margin-bottom: 20rpx;

  .score-item {
    display: flex;
    align-items: center;
    gap: 10rpx;

    .score-label {
      font-size: 26rpx;
      color: #666;
    }

    .score-value {
      font-size: 26rpx;
      font-weight: bold;

      &.high {
        color: #ff4d4f; // Red for high
      }
      &.medium {
        color: #fa8c16; // Orange for medium
      }
      &.low {
        color: #52c41a; // Green for low (assuming lower score is better? adjust if needed)
      }
    }
  }
}

.shop-recommendations {
  background-color: #ffffff;
  padding: 30rpx;
  border-radius: 12rpx;
  margin-bottom: 20rpx;

  .section-title {
    font-size: 30rpx;
    color: #333;
    font-weight: bold;
    margin-bottom: 20rpx;
    display: block;
  }

  .recommend-scroll {
    white-space: nowrap;

    .recommend-item {
      display: inline-block;
      width: 200rpx;
      margin-right: 20rpx;

      &:last-child {
        margin-right: 0;
      }

      .recommend-image {
        width: 200rpx;
        height: 200rpx;
        border-radius: 8rpx;
        margin-bottom: 10rpx;
      }

      .recommend-price {
        display: block;
        text-align: center;
        font-size: 26rpx;
        color: #ff5500;
        font-weight: bold;
      }
    }
  }
}

.contact-seller {
  margin-top: 20rpx;

  .u-button {
    height: 80rpx;
    line-height: 80rpx;
    font-size: 28rpx;
  }
}
</style> 